package com.kx.game.center.servlet;

import com.kx.core.dawn.dbs.dao.DaoManager;
import com.kx.core.dawn.dbs.entity.GameServerEntity;
import com.kx.game.center.common.ServerEnum;
import com.kx.game.center.global.GSServices;
import com.kx.game.center.local.response.LocalErrorResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 加载游戏服信息
 */
@WebServlet(urlPatterns = "/loadGameServerInfo", asyncSupported = true)
public class LoadGameServerServlet extends HttpServlet {

    private static Logger logger = LoggerFactory.getLogger(LoadGameServerServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            GSServices.loadManager.redisState();
            String timeMillis = System.currentTimeMillis() + "";
            //游戏信息
            String serverId = request.getParameter("serverId");
            String createNum = request.getParameter("createNum");
            String ip = request.getParameter("ip");
            String gamePort = request.getParameter("gamePort");
            String gatePort = request.getParameter("gatePort");
            String connectIp = request.getParameter("connectIp");
            String connectPort = "3306";//request.getParameter("connectPort");

            if (ip.equals("0.0.0.0") || ip.equals("127.0.0.1"))
                ip = request.getRemoteHost();
            if (connectIp.equals("0.0.0.0") || connectIp.equals("127.0.0.1"))
                connectIp = request.getRemoteHost();
//            String serverType = request.getParameter("serverType");
//            ServerEnum serverEnum;
//            if (serverType == null || serverType.equals("")) {
//                serverEnum = ServerEnum.indexOf(1);
//            } else
//                serverEnum = ServerEnum.indexOf(Integer.valueOf(serverType));
            //更新DB
            //服务器信息
            if (serverId != null && !serverId.equals("")) {
                GameServerEntity gameServerEntity = DaoManager.gameServerDao.get(serverId);
                if (gameServerEntity == null) {
                    GSServices.gameManager.insert(serverId, 0, Integer.valueOf(createNum),
                            ip, gamePort, gatePort, connectIp, connectPort,
                            ServerEnum.START.getIndex() + "",
                            ServerEnum.GAMER.getIndex() + "",
                            timeMillis);
                } else {
//                    if (!ip.equals(gameServerEntity.getIp())) {
//                        GSServices.localManager.sendMessage(response, false, LocalErrorResponseCode.SERVER_IP_CANT_MATCH, "");
//                        return;
//                    }
                    GSServices.gameManager.update(gameServerEntity, 0, ip, gamePort,
                            gatePort, Integer.valueOf(createNum), timeMillis, connectIp, connectPort);
                }
            }

            GSServices.redisManager.getRedisTemplate().multi();
            GSServices.localManager.deleteGameServerLoginCount(serverId);
            //创建数量
            GSServices.localManager.updateServerCreateNum(serverId, Integer.valueOf(createNum));
            GSServices.localManager.updateGameServerInfo(serverId, ip, gamePort, gatePort, connectIp, connectPort,
                    ServerEnum.START.getIndex() + "",
                    ServerEnum.GAMER.getIndex() + "",
                    timeMillis + "");
            GSServices.redisManager.getRedisTemplate().exec();
            GSServices.gameManager.printLog(request, logger, "Game服务器上线");
            GSServices.localManager.sendMessage(response, true, null, "");
        } catch (Exception e) {
            e.printStackTrace();
            GSServices.localManager.sendMessage(response, false, LocalErrorResponseCode.SYSTEM_ERROR, "");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
